import { Form, Modal } from 'antd'
import React, { useEffect } from 'react'
import Text from '@components/Text'
import FileList from '@components/FileList/Index'
import style from './Index.module.less'

const { Item, useForm } = Form

function initForm (data, formRef) {
  formRef.setFieldsValue(data)
}

function Main ({ id, data, visible, hide, refreshFn }) {
  const [formRef] = useForm()
  useEffect(() => {
    if (formRef && data && visible) {
      initForm(data, formRef)
    }
  }, [formRef, data, visible])
  useEffect(() => {
    if (formRef && !visible) {
      formRef.resetFields()
    }
  }, [visible, formRef])
  return (
    <Modal title='审批详情' width={800} open={visible} onCancel={hide} forceRender footer={null}>
      <Form form={formRef} className={style['detail-form']} labelCol={{ flex: '150px' }}>
        <Item name='type' label='审批类型'>
          <Text />
        </Item>
        <Item name='status' label='状态'>
          <Text />
        </Item>
        <Item noStyle shouldUpdate={(e, c) => e.type !== c.type}>
          {({ getFieldValue }) => {
            const c = getFieldValue('type')
            if (c === '名称变更') {
              return (
                <>
                  <Item name='institution_name_source' label='原名称'>
                    <Text />
                  </Item>
                  <Item name='institution_name_target' label='新名称'>
                    <Text />
                  </Item>
                </>
              )
            }
          }}
        </Item>
        <Item noStyle shouldUpdate={(e, c) => e.file !== c.file}>
          {({ getFieldValue }) => {
            const c = getFieldValue('apply_file')
            if (c && c instanceof Array && c.length) {
              return (
                <>
                  <Item name='apply_file' label='附件'>
                    <FileList />
                  </Item>
                </>
              )
            }
          }}
        </Item>
        <Item label='备注' name='apply_remark'>
          <Text />
        </Item>
        <Item noStyle shouldUpdate={(e, c) => e.status !== c.status}>
          {({ getFieldValue }) => {
            const c = getFieldValue('status')
            const s = getFieldValue('audit_remark')
            if (c === '审核不通过' && s) {
              return (
                <>
                  <Item name='audit_remark' label='审批意见'>
                    <Text />
                  </Item>
                </>
              )
            }
          }}
        </Item>
      </Form>
    </Modal>
  )
}

export default Main
